﻿using LNet;
using OestsCommon.Model;
using OestsServer.Managers;
using OestsServer.Net;

namespace OestsServer.Handlers.UnityHandlers
{
    /// <summary>
    /// 退出登录，但是还在连接状态
    /// </summary>
    public class ExitUnityHandler : HandlerBase<UnityClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.Exit;
            }
        }
        public override void Execute(UnityClientChannel client, Request packet)
        {
            //如果是学生退出，
            Student student = client.User as Student;
            if (student != null)
            {
                //  Student st = client.User as Student;
                //检查学生是否创建了房间 若有销毁掉
                if (student._room != null)
                {
                    RoomManager.DeleteRoom(student._room.RoomId);
                    Response responseDeleteRoom = new Response
                    {
                        ResponseCode = PacketKey.Room,
                        ReturnCode = ReturnCode.Success,
                        ResponseSubCode = SubCode.DeleteRoom,
                        DataObject = student._room.RoomId
                    };
                    UnityClientChannel.Signal_Project.Dispatch(client.ProjectType, responseDeleteRoom);
                }

                student.OnLine = false;
                TeacherManager.Instance.NotifyTeacherStOnlineState(student);//通知管理教员下线
            }
            client.User.DBid = -1;
            client.Send(ResponsePacket);
        }

    }

}
